package com.smartsystem;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.env.Environment;
import java.net.InetAddress;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 智慧超市管理系统主应用程序类
 */
@SpringBootApplication
@MapperScan("com.smartsystem.modules.*.mapper")
@ComponentScan("com.smartsystem")
public class SmartSupermarketApplication {

    public static void main(String[] args) {
        // 创建SpringApplication实例
        SpringApplication app = new SpringApplication(SmartSupermarketApplication.class);
        
        // 关闭默认banner
        app.setBannerMode(Banner.Mode.OFF);
        
        // 打印个性化启动信息
        printCustomBanner();
        
        // 运行应用
        app.run(args);
    }
    
    /**
     * 打印自定义的启动banner
     */
    private static void printCustomBanner() {
        // 定义颜色代码
        String ANSI_GREEN = "\u001B[32m";
        String ANSI_BLUE = "\u001B[34m";
        String ANSI_YELLOW = "\u001B[33m";
        String ANSI_CYAN = "\u001B[36m";
        String ANSI_RESET = "\u001B[0m";
        String ANSI_BOLD = "\u001B[1m";
        
        // 打印美观的启动横幅
        System.out.println(ANSI_BLUE + "╔══════════════════════════════════════════════════════════════════════════════╗" + ANSI_RESET);
        System.out.println(ANSI_BLUE + "║                                                                              ║" + ANSI_RESET);
        System.out.println(ANSI_GREEN + "║    ███████╗███╗   ███╗ █████╗ ██████╗ ████████╗    ███████╗██╗   ██╗███████╗    ║" + ANSI_RESET);
        System.out.println(ANSI_GREEN + "║    ██╔════╝████╗ ████║██╔══██╗██╔══██╗╚══██╔══╝    ██╔════╝╚██╗ ██╔╝██╔════╝    ║" + ANSI_RESET);
        System.out.println(ANSI_GREEN + "║    ███████╗██╔████╔██║███████║██████╔╝   ██║       ███████╗ ╚████╔╝ ███████╗    ║" + ANSI_RESET);
        System.out.println(ANSI_GREEN + "║    ╚════██║██║╚██╔╝██║██╔══██║██╔══██╗   ██║       ╚════██║  ╚██╔╝  ╚════██║    ║" + ANSI_RESET);
        System.out.println(ANSI_GREEN + "║    ███████║██║ ╚═╝ ██║██║  ██║██║  ██║   ██║       ███████║   ██║   ███████║    ║" + ANSI_RESET);
        System.out.println(ANSI_GREEN + "║    ╚══════╝╚═╝     ╚═╝╚═╝  ╚═╝╚═╝  ╚═╝   ╚═╝       ╚══════╝   ╚═╝   ╚══════╝    ║" + ANSI_RESET);
        System.out.println(ANSI_BLUE + "║                                                                              ║" + ANSI_RESET);
        System.out.println(ANSI_BOLD + ANSI_YELLOW + "║                           🏪 智慧超市管理系统 v1.0.0 🏪                          ║" + ANSI_RESET);
        System.out.println(ANSI_BLUE + "║                                                                              ║" + ANSI_RESET);
        System.out.println(ANSI_CYAN + "║                        🚀 高效 • 智能 • 便捷 • 安全                           ║" + ANSI_RESET);
        System.out.println(ANSI_BLUE + "║                                                                              ║" + ANSI_RESET);
        System.out.println(ANSI_BLUE + "╚══════════════════════════════════════════════════════════════════════════════╝" + ANSI_RESET);
        System.out.println(ANSI_YELLOW + "\n🔄 正在初始化系统组件... 欢迎使用智慧超市管理系统，请稍候..." + ANSI_RESET);
        System.out.println();
    }

    /**
     * 应用启动完成后执行的方法，显示成功启动信息
     */
    @Bean
    public ApplicationRunner startupRunner() {
        return args -> {
            // 定义颜色代码
            String ANSI_GREEN = "\u001B[32m";
            String ANSI_BLUE = "\u001B[34m";
            String ANSI_YELLOW = "\u001B[33m";
            String ANSI_CYAN = "\u001B[36m";
            String ANSI_RESET = "\u001B[0m";
            String ANSI_BOLD = "\u001B[1m";
            String ANSI_PURPLE = "\u001B[35m";
            
            try {
                // 获取当前时间
                String currentTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                
                // 获取本机IP地址
                String hostAddress = InetAddress.getLocalHost().getHostAddress();
                
                // 打印成功启动信息
                System.out.println(ANSI_GREEN + "╔══════════════════════════════════════════════════════════════════════════════╗" + ANSI_RESET);
                System.out.println(ANSI_GREEN + "║                                                                              ║" + ANSI_RESET);
                System.out.println(ANSI_BOLD + ANSI_GREEN + "║                          🎉 应用启动成功！🎉                                  ║" + ANSI_RESET);
                System.out.println(ANSI_GREEN + "║                                                                              ║" + ANSI_RESET);
                System.out.println(ANSI_CYAN + "║  📅 启动时间: " + currentTime + "                                           ║" + ANSI_RESET);
                System.out.println(ANSI_YELLOW + "║  � 本地访问:  http://localhost:8080/api                                    ║" + ANSI_RESET);
                System.out.println(ANSI_PURPLE + "║  � 接口文档: http://localhost:8080/doc.html                               ║" + ANSI_RESET);
                System.out.println(ANSI_PURPLE + "║  � 数据监控: http://localhost:8080/druid/index.html (admin/admin123)    ║" + ANSI_RESET);
                System.out.println(ANSI_GREEN + "║                                                                              ║" + ANSI_RESET);
                System.out.println(ANSI_BLUE + "║                        💡 系统已准备就绪，开始您的智慧之旅！                        ║" + ANSI_RESET);
                System.out.println(ANSI_GREEN + "║                                                                              ║" + ANSI_RESET);
                System.out.println(ANSI_GREEN + "╚══════════════════════════════════════════════════════════════════════════════╝" + ANSI_RESET);
                
                System.out.println(ANSI_YELLOW + "\n💡 系统已准备就绪，开始您的智慧之旅！" + ANSI_RESET);
                System.out.println();
                
            } catch (Exception e) {
                System.out.println(ANSI_GREEN + "✅ 智慧超市管理系统启动成功！" + ANSI_RESET);
                System.out.println(ANSI_YELLOW + "🔗 访问地址: http://localhost:8080/api" + ANSI_RESET);
                System.out.println(ANSI_PURPLE + "📚 接口文档: http://localhost:8080/doc.html" + ANSI_RESET);
            }
        };
    }
}